package bd;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import visual.CrearPartida.VentanaPartida;

import Ajedrez.Control.Jugador;
import Ajedrez.Control.movimiento;
import Ajedrez.Control.pieza;

import com.sun.corba.se.spi.orb.Operation;
public class JDBC1 //implements JDBCi
{
		
	static Connection connection;
	ResultSet resulset;
	
		public JDBC1() throws SQLException, ClassNotFoundException{
			Class.forName("org.sqlite.JDBC");
			connection=DriverManager.getConnection("jdbc:sqlite:C:/workspace/Z/Ajedrez.sqlite");
					
		}
		public Collection<Operation> getAllCalculatedOperations() throws Exception{
			return null;
			
		}
		
		public int insertJugador(String j1,String j2,Jugador j) throws SQLException, ClassNotFoundException {
			Statement statement = null;
			VentanaPartida v = new VentanaPartida();
			statement =  connection.createStatement();
			String sql = "INSERT INTO jugadores(cod_jug,nombre,fecha,hora,num_jugador,cod_mov) VALUES("+j.getCod_jug()+"," +"'"+v.getTf()+"'"+","+"'"+  j.getFecha()+"'"+"," +j.getHora()+"," +j.getNum_jug()+"," +j.getCod_mov()+")";
			statement.executeUpdate(sql);
			statement.close();
			
			return 0;
		}
		public int insertJugador2(Jugador j) throws SQLException, ClassNotFoundException {
			Statement statement = null;
			statement =  connection.createStatement();
			VentanaPartida v = new VentanaPartida();
			String sql = "INSERT INTO jugadores(cod_jug,nombre,fecha,hora,num_jugador,cod_mov) VALUES("+j.getCod_jug()+"," +"'"+v.getTf()+"'"+","+"'"+  j.getFecha()+"'"+"," +j.getHora()+"," +j.getNum_jug()+"," +j.getCod_mov()+")";
			statement.executeUpdate(sql);
			statement.close();
			return 0;
		}	
		
	
		public int insert(movimiento m) throws SQLException {
			Statement statement = null;
			pieza p = new pieza();
			statement = connection.createStatement();
			String sql = "INSERT INTO movimiento(color,pieza,posicionx,fecha,num_jugador,posiciony) VALUES('"+m.getColor()+"',"+p.getCod_pieza()+","+m.getPosx()+","+m.getFecha()+","+m.getNum_jug()+","+m.getPosy()+")";
			int result =  statement.executeUpdate(sql);
			statement.close();
			return result;
		}
			public int remove(movimiento m,int x, int y) throws SQLException {
			Statement statement = connection.createStatement();
			String sql = "DELETE FROM movimiento WHERE posx="+m.getPosx()+"and posy="+m.getPosy();
			statement.executeUpdate(sql);
			statement.close();		
			
			return 0;
		}
		public static int modify(movimiento m) throws SQLException {
			Statement statement = connection.createStatement();
			String sql = "UPDATE movimiento SET color = 'blanco' WHERE cod_mov = 9";
			statement.executeUpdate(sql);
			statement.close();
			return 0;
		}
		public Collection<movimiento> getAll(movimiento m) throws SQLException {
			java.sql.Statement statement  = connection.createStatement();
			String sql = "Select * from movimiento";
			resulset = statement.executeQuery(sql);
			ArrayList <movimiento> mov = new ArrayList <movimiento>();
			statement.close();
			return mov;
		}
		
		
		
		
		
	
	}

